package farm.entity;

import farm.pattern.chain.SafetyHandler;
import farm.pattern.observer.Observer;
import farm.pattern.state.MachineState;
import farm.pattern.strategy.PathStrategy;

public abstract class FarmMachine implements Observer {
    private String type;
    private MachineState state;
    private PathStrategy pathStrategy;
    private SafetyHandler safetyChain;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public MachineState getState() {
        return state;
    }

    public void setState(MachineState state) {
        this.state = state;
    }

    public PathStrategy getPathStrategy() {
        return pathStrategy;
    }

    public void setPathStrategy(PathStrategy pathStrategy) {
        this.pathStrategy = pathStrategy;
    }

    public SafetyHandler getSafetyChain() {
        return safetyChain;
    }

    public void setSafetyChain(SafetyHandler safetyChain) {
        this.safetyChain = safetyChain;
    }

    @Override
    public void update(EnvironmentData data) {
        // 仅当农机处于运行状态时处理环境数据
        if (state.isRunning()) {
            System.out.println(type + " 接收到环境数据更新");
            handleEnvironmentChange(data);
        }
    }

    private void handleEnvironmentChange(EnvironmentData data) {
        safetyChain.handleRequest(data, this);  // 责任链处理
        String path = pathStrategy.planPath(data);  // 路径规划
        System.out.println(type + " 路径规划结果：" + path);
    }
}